﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Thksoft.Module.DataDetail;

namespace Thksoft.UserCotrols
{
    public partial class PlanDetailThree : BaseControl
    {
        #region 页面参数

        public int proType { get; set; }

        #endregion 页面参数

        public DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            //BindData();
        }

        //安装外包费用月明细表(T3)
        public void BindData()
        {
            var pPage = this.Page as ProjectFundDetail;

            lblyear.Text = pPage.myDate;
            lbldw.Text = pPage.cmyName;
            lblTime.Text = pPage.valTime;
            //lblUser.Text = pPage.valUser;
            if (pPage.dtYear.Rows.Count > 0)
            {
                FORM_KEY1.Value = pPage.dtYear.Rows[0]["N_YEAR"].ToString();
                FORM_KEY2.Value = pPage.dtYear.Rows[0]["N_MONTH"].ToString();
                FORM_KEY3.Value = pPage.dtYear.Rows[0]["N_WEEK"].ToString();
            }

            switch (proType)
            {
                case 1: lblProjectName.Text = "安装外包费用周明细表"; break;
                case 2: lblProjectName.Text = "材料费用周明细表"; break;
                case 3: lblProjectName.Text = "大修费用周明细表"; break;
                default: lblProjectName.Text = "城市管网项目周明细表"; break;
            }
            dt = new QS_PLAN_DETAIL3_BLL().GetTable(pPage.planappId, 3, proType);

            var row = dt.NewRow();

            row["C_CUSTOMER_NAME"] = "合计";

            row["N_GZAMOUNT"] = dt.AsEnumerable().Sum(a => a["N_GZAMOUNT"] == DBNull.Value ? 0 : Convert.ToDecimal(a["N_GZAMOUNT"]));

            dt.Rows.Add(row);

            ViewState["database"] = dt;
            grvShow3.DataSource = dt;
            grvShow3.DataBind();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["ORDER"] == null ? null : (string)ViewState["ORDER"];
            OrderDire = ViewState["ORDER_TYPE"] == null ? null : (string)ViewState["ORDER_TYPE"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["ORDER"] = SortOrder;
            ViewState["ORDER_TYPE"] = OrderDire;
        }

        public void BindDaodu()
        {
            //var dt = (DataTable)ViewState["database"];
            //dt.Columns["r"].ColumnName = "序号";
            //dt.Columns["C_CUSTOMER_NAME"].ColumnName = "单位名称";
            //dt.Columns["C_AMOUT_TYPE"].ColumnName = "款项类别";
            //dt.Columns["C_AMOUT_USE"].ColumnName = "款项用途";
            //dt.Columns["N_GZAMOUNT"].ColumnName = "挂账金额（不含质保金）";
            //dt.Columns["N_ZBAMOUNT"].ColumnName = "质保金";
            //dt.Columns["N_MONTHJEUSE"].ColumnName = "本月累计支付金额";
            //dt.Columns["N_PLANAMOUNT"].ColumnName = "本月上报资金计划";
            //dt.Columns["N_SPAMOUNT1"].ColumnName = "各单位审批金额";
            //dt.Columns["N_SPAMOUNT2"].ColumnName = "总部资金审批金额";
            //dt.Columns["C_BANK"].ColumnName = "银行现金流向";
            //dt.Columns["C_REMARK"].ColumnName = "备注";
            //dt.Columns.Remove("N_ID3");
            //dt.Columns.Remove("N_PRO_ID");
            //dt.Columns.Remove("N_TYPE");
            //dt.Columns.Remove("N_PLANAPP_ID");
            //dt.Columns.Remove("N_PROTYPE");
            //dt.Columns.Remove("N_CUSTOMER_ID");
            //dt.Columns.Remove("N_REAMOUNT");
            //dt.Columns.Remove("C_PRONAME");
            //dt.Columns.Remove("N_NUM");

            var execll = new BasePage();
            //execll.toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + lblProjectName.Text);
            execll.toExecl1(Panel1, lblProjectName.Text + DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Millisecond.ToString());
        }

        public void BindPrint()
        {
            var key = Guid.NewGuid().ToString();
            Session[key] = FORM_PRINT_VALUE.Value;
            MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=HTML&VALUE=" + key + "\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
        }
    }
}